<template>
  <div class="departments-container">
    <!-- 提供了一些默认的内边距 padding -->
    <div class="app-container">
      <el-card v-loading="loading" class="tree-card">
        <!-- 用了一个行列布局 -->
        <TreeTools :node-data="company" :root="true" @addDepts="addDepts" />

        <el-tree
          :data="departs"
          :props="defaultProps"
          :default-expand-all="true"
        >
          <template
            #default="{ data }"
          ><TreeTools
            :node-data="data"
            @delDepts="getDeparts"
            @addDepts="addDepts"
            @editDepts="editDepts"
          /></template>
        </el-tree>
      </el-card>
      <AddDept
        ref="addDept"
        :is-show="isShow"
        :dept-list="deptList"
        :node-data="nodeData"
        :close-dialog.sync="isShow"
        @addDepts="getDeparts"
      />
    </div>
  </div>
</template>

<script>
import TreeTools from './components/TreeTools.vue'
import AddDept from './components/AddDept.vue'
import { reqGetDeparts } from '@/api/departments'

import { transListToTree } from '@/utils'
export default {
  name: 'Departments',
  components: {
    TreeTools,
    AddDept
  },
  data() {
    return {
      departs: [],
      defaultProps: {},
      company: { name: '江苏传智播客教育科技股份有限公司', manager: '负责人' },
      isShow: false,
      deptList: [],
      nodeData: {},
      loading: false
    }
  },
  computed: {},
  created() {
    this.getDeparts()
  },
  methods: {
    async getDeparts() {
      this.loading = true
      await new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve()
        }, 2000)
      })

      const {
        data: { companyName, depts }
      } = await reqGetDeparts()
      // console.log(depts)
      this.deptList = depts
      this.company = {
        name: companyName,
        manager: '负责人',
        id: ''
      }
      this.departs = transListToTree(depts, '')
      this.loading = false
      // console.log(this.departs)
    },

    addDepts(data) {
      this.isShow = true
      console.log(data)
      this.nodeData = data
    },
    editDepts(data) {
      this.isShow = true
      this.nodeData = data
      this.$nextTick(() => {
        this.$refs.addDept.getDeptsDetails()
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.el-tree {
  ::v-deep {
    // 小三角的样式, 去掉默认的小三角的旋转效果
    .el-tree-node__expand-icon.expanded {
      -webkit-transform: rotate(0deg);
      transform: rotate(0deg);
    }
    // 有子节点 且未展开 +
    .el-icon-caret-right:before {
      background: url("~@/assets/common/add-circle.png") no-repeat 0 0;
      content: "";
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 有子节点 且已展开 -
    .el-tree-node__expand-icon.expanded.el-icon-caret-right:before {
      background: url("~@/assets/common/minus-circle.png") no-repeat 0 0;
      content: "";
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 没有子节点
    .el-tree-node__expand-icon.is-leaf::before {
      background: url("~@/assets/common/user-filling.png") no-repeat 0 0;
      content: "";
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
  }
}
</style>
